package com.tengfei.api.course;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.tengfei.common.annotation.Log;
import com.tengfei.common.annotation.RepeatSubmit;
import com.tengfei.common.core.controller.BaseController;
import com.tengfei.common.core.domain.PageQuery;
import com.tengfei.common.core.domain.R;
import com.tengfei.common.core.page.TableDataInfo;
import com.tengfei.common.core.validate.AddGroup;
import com.tengfei.common.core.validate.EditGroup;
import com.tengfei.common.enums.BusinessType;
import com.tengfei.main.course.domain.bo.CourseBo;
import com.tengfei.main.course.domain.vo.CourseVo;
import com.tengfei.main.course.service.ICourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;

/**
 * 课程Controller
 *
 * @author ruoyi
 * @date 2022-07-27
 */
@Validated
@Api(value = "课程控制器", tags = {"课程管理"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/course/course")
public class CourseController extends BaseController {

    private final ICourseService iCourseService;

    /**
     * 查询课程简易列表信息
     */
    @ApiOperation("查询课程简易列表信息")
    @SaCheckPermission("course:course:list")
    @GetMapping("/simpleList")
    public R<List<Map<String, Object>>> simpleList() {
        return R.ok(iCourseService.querySimpleList());
    }

    /**
     * 查询课程列表
     */
    @ApiOperation("查询课程列表")
    @SaCheckPermission("course:course:list")
    @GetMapping("/list")
    public TableDataInfo<CourseVo> list(CourseBo bo, PageQuery pageQuery) {
        return iCourseService.queryPageList(bo, pageQuery);
    }

    /**
     * 获取课程详细信息
     */
    @ApiOperation("获取课程详细信息")
    @SaCheckPermission("course:course:query")
    @GetMapping("/{courseId}")
    public R<CourseVo> getInfo(@ApiParam("课程id")
                               @NotNull(message = "主键不能为空")
                               @PathVariable("courseId") Long courseId) {
        return R.ok(iCourseService.queryById(courseId));
    }

    /**
     * 新增课程
     */
    @ApiOperation("新增课程")
    @SaCheckPermission("course:course:add")
    @Log(title = "新增课程", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody CourseBo bo) {
        return toAjax(iCourseService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改课程
     */
    @ApiOperation("修改课程")
    @SaCheckPermission("course:course:edit")
    @Log(title = "修改课程", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody CourseBo bo) {
        return toAjax(iCourseService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 批量删除课程
     */
    @ApiOperation("批量删除课程")
    @SaCheckPermission("course:course:remove")
    @Log(title = "批量删除课程", businessType = BusinessType.DELETE)
    @DeleteMapping("/")
    public R<Void> remove(@ApiParam("课程id数组")
                          @NotEmpty(message = "课程id数组不能为空")
                          @RequestParam("courseIds[]") List<Long> ids) {
        return toAjax(iCourseService.deleteWithValidByIds(ids) ? 1 : 0);
    }
}
